package com.topdt.jbpm.web.struts.action;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.thx.common.struts2.CurdAction;
import com.thx.common.util.AppContext;
import com.thx.common.web.ActionResult;
import com.thx.log.LogUtil;
import com.topdt.jbpm.model.Bookmark;
import com.topdt.jbpm.service.BookmarkService;
import com.topdt.jbpm.utils.ModuleConstant;

/**  
 *  @Project       : thx-workflow
 *  @Program Name  : com.topdt.jbpm.web.struts.action.BookmarkAction.java
 *  @Class Name    : BookmarkAction
 *  @Description   : 公文管理/标签
 *  @Author        : calonlan
 *  @Creation Date : 2015年4月13日 上午11:10:28 
 *  @ModificationHistory  
 *  Who        When          What 
 *  --------   ----------    -----------------------------------
 *  username   2015年4月13日       TODO
 */
@Namespace("/workflow")
@Action(value = "bookmark", results = {
		@Result(name = "list", location = "/workflow/bookmark/list.jsp"),
		@Result(name = "input", location = "/workflow/bookmark/input.jsp"),
		@Result(name = "json", type = "json", params = { "root", "result" })
})
@Controller
@Scope("prototype")
public class BookmarkAction extends CurdAction<Bookmark> {
	
	private static final long serialVersionUID = 1694942610550085428L;
	
	@Autowired
	private BookmarkService bookmarkService;

	@Override
	protected void doList() throws Exception {
		logger.debug("<进入>:doSave方法   标签列表");
		list = bookmarkService.findByDescripton(entity.getDescription());
	}

	@Override
	protected void doInput() throws Exception {
		logger.debug("<进入>:doInput方法");
		if (StringUtils.isNotBlank(id)) {
			entity = bookmarkService.get(Integer.parseInt(id));
			if (entity == null) {
				setResult(ActionResult.FAIL, "要修改的记录不存在！");
			}
		}
	}

	@Override
	protected void doSave() throws Exception {
		logger.debug("<进入>:doSave方法   新增或修改标签");
		if (StringUtils.isBlank(id)) {
			entity.setCompanyId(AppContext.getCurrentComId());
			bookmarkService.save(entity);
			setResult(ActionResult.SUCCESS, "添加成功！");
			LogUtil.log(ModuleConstant.BOOKMARK, LogUtil.ACTION_ADD, LogUtil.RESULT_SUCCESS, "新增标签");
		} else {
			Bookmark bookmark = bookmarkService.get(Integer.parseInt(id));
			bookmark.setName(entity.getName());
			bookmark.setDescription(entity.getDescription());
			bookmark.setText(entity.getText());
			bookmarkService.update(bookmark);
			setResult(ActionResult.SUCCESS, "修改成功！");
			LogUtil.log(ModuleConstant.BOOKMARK, LogUtil.ACTION_UPDATE, LogUtil.RESULT_SUCCESS, "修改标签");
		}
	}

	@Override
	protected void doDelete() throws Exception {
		logger.debug("<进入>:doDelete方法   删除标签");
		if (StringUtils.isNotBlank(id)) {
			bookmarkService.delete(Integer.parseInt(id));
			setResult(ActionResult.SUCCESS, "删除成功！");
			LogUtil.log(ModuleConstant.BOOKMARK, LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "删除标签");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	@Override
	protected void doDelMulti() throws Exception {
		logger.debug("<进入>:doDelMulti方法   删除标签");
		if (delId != null && delId.length > 0) {
			delId = delId[0].split(",");
			for (String id : delId) {
				bookmarkService.delete(Integer.parseInt(id));
			}
			setResult(ActionResult.SUCCESS, "删除成功！");
			LogUtil.log(ModuleConstant.BOOKMARK, LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "删除标签");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}
	
	public void checkBookmarkName() {
		logger.debug("<进入>:checkBookmarkName方法");
		List<Bookmark> list = bookmarkService.findByName(entity.getName());
		try {
			if (list == null || list.size() <= 0) {
				writeJson("true");
			} else {
				if (list.get(0).getBookmarkId().equals(Integer.parseInt(id))) {
					writeJson("true");
				} else {
					writeJson("false");
				}
			}
		} catch (IOException e) {
			logger.error("<错误>:检查标签名失败",e);
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

}
